#
# Print a Linux Counter error form
#
package ErrorForm;
use CGI;
use CounterCGI;
require Exporter;
@ISA = qw(Exporter);

@EXPORT = qw(errorForm);

$q = new CGI;

sub errorForm {
    my $why = shift;
    my $arg = shift;
    my $explain = shift;
    my $nonspecific;

#print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\n";

print "Content-type: text/html; charset=\"utf-8\"\n\n";
print "<html>";
CounterCGI::pagehead(CounterCGI::__("Linux Counter Error message") . " " . $why);
print "<body>\n";
CounterCGI::pagetop($q);
print "<h1><img src=\"/gifs/Logo-small.png\" alt=\"Linux Counter Logo\">" . CounterCGI::__("Error") . "</h1>";
warn "WHY equals \"$why\"";
print "<div class=\"message\">";
    if ($why eq "bogon value") {
	print CounterCGI::__("One of your fields contained a control character. Please check carefully and correct it!") . "\n";
    } elsif ($why eq "illegal email") {
	print CounterCGI::__("Your E-mail address") . ", $arg, " . CounterCGI::__("does not seem to be correct. Check it carefully; if you believe it is correct, please send E-mail to the maintainer.") . "\n";
	if ($explain) {
	    print "<p>\n" . CounterCGI::__("The error message was") . ":<br><br>\n$explain\n";
	}
    } elsif ($why eq "duplicate email") {
	print CounterCGI::__("The E-mail address you gave") . ", $arg, " . CounterCGI::__("cannot be entered into the database, probably because you are already registered with the counter. Please use the") . " <a href=\"$counter_site/person/login.php\">" . CounterCGI::__("login page") . "</a> " . CounterCGI::__("to log in and edit your entry, or to get your key sent to you, if you believe that you may already be entered into the database.");
    } elsif ($why eq "no email") {
        print CounterCGI::__("You did not enter an E-mail address into the \"Identification\" field.  This is REQUIRED in order to register with the counter.") .
"\n<br>\n" .
CounterCGI::__("These data are <b>not</b> published, but the counter requires this so that you can be contacted if there are any questions related to your entry.") .
"\n<p>\n" .
CounterCGI::__("Please use the \"back\" button of your browser to go back to the form and fill in this value.");
    } elsif ($why eq "bogus key") {
	print CounterCGI::__("The key you entered is not a valid Linux Counter user key. Please try again.") . "\n<p>\n" .
CounterCGI::__("The key is listed in the mail you got from the Linux Counter, on the line saying \"//KEY\", and looks something like \"12345-67890\".") .
"\n<p>\n" .
CounterCGI::__("If you do not have the key, and want to register a machine, try using your email address.");
    } elsif ($why eq "bogus password") {
	print CounterCGI::__("The password you entered is not correct. Please try again.") . "\n<p>\n" .
CounterCGI::__("The password is listed in the mail you got from the Linux Counter, on the line saying \"//KEY\", and looks something like \"12345-67890\".") .
"\n<p>\n" .
CounterCGI::__("If you do not have the password, please try the \"Email me!\" button on the login page.");
    } elsif ($why eq "no such user") {
	print CounterCGI::__("No such user number is registered in the database.<br> Please try another.");
    } elsif ($why eq "no key") {
	print CounterCGI::__("The key field you gave was empty. Please go back and fill it in.");
    } elsif ($why eq "private info") {
        print CounterCGI::__("User $arg has not permitted the requested information to be made public.");
    } elsif ($why eq "text") {
	# Caller specified text. This is not very nice...
	print $arg;

    } elsif ($why eq "no cookies") {
	# From login: No cookie support
	print CounterCGI::__("Your browser does not seem to be configured for cookie support. <p>At the moment, the counter does not support login without cookies. We are working to fix this, but it is not ready yet. <p>Your login is correct, and your entry will be preserved by the Linux Counter. Please either enable cookies, or come back later, and see if we have managed to make it work for you. <p> Please contact help&#x40;counter.li.org if there is any problem.");
        warn "Cookie diagnostic: Browser: $ENV{HTTP_USER_AGENT}\n";
	warn "Cookie diagnostic: Via: $ENV{HTTP_VIA}\n" if $ENV{HTTP_VIA};
    } else {
        print CounterCGI::__("The error:<br><br>$why<br><br>occured. Please report to") . " help&#x40;counter.li.org " . CounterCGI::__("and/or visit the") . " <a href=\"$counter_site/help/faq.php\">" . CounterCGI::__("FAQ") . "</a> " . CounterCGI::__("to see if we already mentioned your problem there.") . "\n";
	$nonspecific = 1;
    }
    print <<EoF;
</div>
<p>
<hr>
EoF
print CounterCGI::pagebottom("yes");
print <<EoF;
</body>
</html>
EoF
    if ($explain) {
	warn ">>>>$0 ErrorForm exiting on $why($arg) $explain\n";
    } elsif ($arg) {
	warn ">>>>$0 ErrorForm exiting on $why($arg)\n";
    } else {
	warn ">>>>$0 ErrorForm exiting on $why\n";
    }
    if ($nonspecific) {
	warn ">>>>Error was nonspecific\n";
    }
    exit 0; # Just terminate - this simplifies higher levels
}
